package com.eight.group.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.eight.group.entity.QueryPageBean;
import com.eight.group.entity.Result;
import com.eight.group.utils.DateUtils;
import com.xqx.eight.group.service.MemberManagerService;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/_member")
public class MemberController {
    @Reference
    private MemberManagerService service;

    //查询
    @RequestMapping("findPage.do")

    public List<Map<String, Object>> findPage(@RequestBody QueryPageBean bean) throws Exception {
        List<Map<String, Object>> page = service.findPage(bean);
        return page;
    }

    //删除
    @RequestMapping("delete.do")
    public Result delect(int id) {
        try {
            Result delete = service.delete(id);
            return delete;
        } catch (Exception e) {
            return new Result(false, "删除会员失败!");
        }
        // return new Result(true,"删除会员成功!" );
    }

    //添加
    @RequestMapping("/add.do")
    public Result add(@RequestBody Map<String, Object> map) {
        try {
            service.add(map);
            return new Result(true, "添加成功!");
        } catch (Exception e) {
            return new Result(false, "添加失败!");
        }
    }

    @RequestMapping("/findById.do")
    public Result findById(int id) {
        try {
            Map<String, Object> mapList = service.findById(id);
            //   System.out.println(mapList);
            return new Result(true, "查询成功!", mapList);
        } catch (Exception e) {
            return new Result(false, "查询失败");
        }
    }

    @RequestMapping("/findAllHealthManager.do")
    public Result findAllHealthManager() {
        try {
            //   System.out.println(mapList);
            return new Result(true, "查询健康管理师列表成功!", service.findAllHealthManager());
        } catch (Exception e) {
            return new Result(false, "查询健康管理师列表失败");
        }
    }

    @RequestMapping("/edit.do")
    public Result lihuan(@RequestBody Map<String, Object> map) {
        try {
            service.edit(map);
            return new Result(true, "修改成功!");
        } catch (Exception e) {
            return new Result(false, "修改失败");
        }
    }

    //档案号	姓名	性别	年龄	手机号码	注册时间	健康管理师
    @RequestMapping("excel.do")
    public void excel(HttpServletRequest request, HttpServletResponse response) {
        try {
            List<Map<String, Object>> excels = service.findExcel();
            //获取文件的绝对路劲名字
            String strname = request.getSession().getServletContext().getRealPath("template")
                    + File.separator + "member.xlsx";
            //读取模板文件创建excel表格
            XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(new File(strname)));
            XSSFSheet sheet = workbook.getSheetAt(0);
            for (int i = 0; i < excels.size(); i++) {
                int s = i + 1;
                XSSFRow row = sheet.createRow(s);
                if (excels.get(i).get("FileNo") != null) {
                    row.createCell(0).setCellValue((String) excels.get(i).get("FileNo"));
                } else {
                    row.createCell(0).setCellValue("");
                }
                if (excels.get(i).get("name") != null) {
                    row.createCell(1).setCellValue((String) excels.get(i).get("name"));
                } else {
                    row.createCell(1).setCellValue("");
                }
                if (excels.get(i).get("sex") != null) {
                    Integer sexNum = (Integer) excels.get(i).get("sex");
                    if (sexNum.equals(1)) {
                        row.createCell(2).setCellValue("男");
                    } else {
                        row.createCell(2).setCellValue("女");
                    }
                } else {
                    row.createCell(2).setCellValue("");
                }
                if (excels.get(i).get("age") != null) {
                    row.createCell(3).setCellValue((Long) excels.get(i).get("age"));
                } else {
                    row.createCell(3).setCellValue("");
                }
                if (excels.get(i).get("phoneNumber") != null) {
                    row.createCell(4).setCellValue((String) excels.get(i).get("phoneNumber"));
                } else {
                    row.createCell(4).setCellValue("");
                }
                if (excels.get(i).get("regTime") != null) {
                    row.createCell(5).setCellValue(DateUtils.parseDate2String((Date) excels.get(i).get("regTime")));
                } else {
                    row.createCell(5).setCellValue("");
                }
                if (excels.get(i).get("username") != null) {
                    row.createCell(6).setCellValue((String) excels.get(i).get("username"));
                } else {
                    row.createCell(6).setCellValue("");
                }
            }
            ServletOutputStream out = response.getOutputStream();
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("content-Disposition", "attachment;filename=member.xlsx");
            workbook.write(out);
            out.flush();
            out.close();
            workbook.close();
            // return null;

        } catch (Exception e) {
            e.printStackTrace();
            // return new Result(false,MessageConstant.GET_BUSINESS_REPORT_FAIL,null);
        }
    }
}
